﻿<?php

$test = validate();
if ($test) {
	if(isset($_POST['action']))
	{
		$action = $_POST['action'];
			
		if($action == "create_account") {
			include('account_registration_recap.php');
		} else {
			$conn = unserialize($_SESSION['connexion']);
			$q = new ClientQuery();
			$client = $q->findOneByEmail($conn->login);

			$addr = AdresseQuery::create()->findPk($client->getAdresse());

			if($client->getFacturation() == NULL){
				$addrDelivery = $addr;
			} else {
				$addrDelivery = AdresseQuery::create()->findPk($client->getFacturation());
			}

			$client->setEmail($_POST['mail']);
			$client->setPassword(md5($_POST['password']));
			$client->save();
			
			$addr->setTitre($_POST['title']);
			$addr->setNom($_POST['name']);
			$addr->setPrenom($_POST['firstname']);
			$addr->setAdresse($_POST['address']);
			$addr->setPostal($_POST['postalCode']);
			$addr->setVille($_POST['city']);
			$addr->setTel($_POST['phone']);

			if(!isset($_POST['deliveryAdress'])){				
				$addrDelivery->setTitre($_POST['title']);
				$addrDelivery->setNom($_POST['nameDelivery']);
				$addrDelivery->setPrenom($_POST['firstnameDelivery']);
				$addrDelivery->setAdresse($_POST['addressDelivery']);
				$addrDelivery->setPostal($_POST['postalCodeDelivery']);
				$addrDelivery->setVille($_POST['cityDelivery']);
				$addrDelivery->setTel($_POST['phoneDelivery']); 
			} else {
				$addrDelivery->setTitre($_POST['title']);
				$addrDelivery->setNom($_POST['name']);
				$addrDelivery->setPrenom($_POST['firstname']);
				$addrDelivery->setAdresse($_POST['address']);
				$addrDelivery->setPostal($_POST['postalCode']);
				$addrDelivery->setVille($_POST['city']);
				$addrDelivery->setTel($_POST['phone']); 
			}
			
			$addr->save();
			$addrDelivery->save();
			
			include('account.php');
		}
	}
} else {
	include('account_edit.php');
}

//*****************************************************************************
//Validate Password
//*****************************************************************************
function validatePassword($var)
{
    $password = $_POST[$var];
    $exprPassword = "#^.{6,10}$#";
    return preg_match($exprPassword,$password);
}


//*****************************************************************************
//Validate Confirmation
//*****************************************************************************
function validateConfirmation($var1, $var2)
{
	$password = $_POST[$var1];
	$confirmation = $_POST[$var1];
    
    return $password == $confirmation;
}


//*****************************************************************************
//Validate Emptyness
//*****************************************************************************
function validateEmptyField($var)
{
   $field = $_POST[$var];
   $exprField = "#^.+$#";
   
   return preg_match($exprField,$field);
}

//*****************************************************************************
//Validate Phone
//*****************************************************************************
function validatePhone($var)
{
    $phone = $_POST[$var];
    $exprPhone = "#^[0-9]{10}$#";
    return preg_match($exprPhone,$phone);
}

//*****************************************************************************
//Validate Postal Code
//*****************************************************************************
function validatePostalCode($var)
{
    $postal = $_POST[$var];
    $exprPostal = "#^[0-9]{5}$#";
    return preg_match($exprPostal,$postal);
}

//*****************************************************************************
//Validate Name
//*****************************************************************************
function validateName($var)
{
    $name = $_POST[$var];
    $exprName = "#^[a-zA-Z]+$#";
    return preg_match($exprName,$name);
}

//*****************************************************************************
//Validate Firstname
//*****************************************************************************
function validateFirstname($var)
{
    $firstname = $_POST[$var];
    $exprFirstname = "#^[a-zA-Z]+$#";
    return preg_match($exprFirstname,$firstname);
}


//*****************************************************************************
//Validate Form
//*****************************************************************************
function validate()
{
    $res = true;
    $res = validateEmptyField('mail') && $res;
    $res = validatePassword('password') && $res;
    $res = validateConfirmation('password','confirmation') && $res;
    $res = validateName('name') && $res;
    $res = validateFirstname('firstname') && $res;
    $res = validateEmptyField('address') && $res;    
    $res = validatePostalCode('postalCode') && $res;
    $res = validateEmptyField('city') && $res;    
    $res = validatePhone('phone') && $res;
	
    if(!isset($_POST['deliveryAdress']))
    {
        $res = validateName('nameDelivery') && $res;
        $res = validateFirstname('firstnameDelivery') && $res;
        $res = validateEmptyField('addressDelivery') && $res;    
        $res = validatePostalCode('postalCodeDelivery') && $res;
        $res = validateEmptyField('cityDelivery') && $res;    
        $res = validatePhone('phoneDelivery') && $res;
    }

    return $res;
}


